/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.java.net;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
/**
 * 
 * @author Sibaozuan(superskysbz@gmail.com)
 *
 */
public class SocketSummary {
	private String path = "";
	private static final String SERVER = "192.168.0.116";
	private static final Integer PORT = 54321;

	public SocketSummary(String path) {
		this.path = path;
	}

	/**
	 * 
	 * @param msg
	 */
	public void testURL(String msg) {
		try {
			URL url = new URL(path);
			String protocol = url.getProtocol();
			String host = url.getHost();
			int port = url.getPort();
			String file = url.getFile();
			System.out.println("url: " + protocol + "/" + host + "/" + port + "/" + file);
			InputStream mInputStream = url.openStream();
			BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(
					mInputStream));
			String str;
			while ((str = mBufferedReader.readLine()) != null) {
				System.out.println("str: " + str);
			}
			mBufferedReader.close();
			mInputStream.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param msg
	 */
	public void testInetAddress(String msg) {
		try {
			InetAddress address = InetAddress.getByName(SERVER);
			System.out.println("��" + address.getHostName() + "请求连接");
			Socket client = new Socket(address, PORT);
			System.out.println("���ӳɹ���");
			OutputStream outputStream = client.getOutputStream();
			outputStream.write(msg.getBytes());
			outputStream.close();
			client.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param msg
	 */
	public void testSocket(String msg) {
		try {
			Socket client = new Socket(SERVER, PORT);
			System.out.println("连接成功");
			PrintWriter writer = new PrintWriter(client.getOutputStream());
			writer.println("");
			//
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			reader.readLine();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param msg
	 */
	public void testServerSocket(String msg) {
		try {
			ServerSocket serverSocket = new ServerSocket(PORT);
			while (true) {
				Socket client = serverSocket.accept();// 阻塞等待
				DataInputStream dataInputStream = new DataInputStream(
						client.getInputStream());
				dataInputStream.read();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
